﻿#ifdef WX_PRECOMP
#include "wx_pch.h"
#endif

#ifdef __BORLANDC__
#pragma hdrstop
#endif //__BORLANDC__

#include "CEFrame.h"
#include "CECascadeSystem.h"

#include <wx/colordlg.h> 

namespace
{
	///------------------------------------------------------------
	// TitleWindow
	///------------------------------------------------------------
	class TitleWindow : public wxPanel
	{
	public:
		TitleWindow( wxWindow* parent, wxString title );
		virtual ~TitleWindow(){}
		wxPanel* GetClientPanel(){ return m_pClientPanel; }
	protected:
		UINT m_nBarHeight;
		wxPanel* m_pTitleBar;
		wxPanel* m_pClientPanel;
		void OnSize( wxSizeEvent& );
		DECLARE_EVENT_TABLE()
	};

	BEGIN_EVENT_TABLE( TitleWindow, wxPanel )
		EVT_SIZE( TitleWindow::OnSize )
	END_EVENT_TABLE( )

	TitleWindow::TitleWindow( wxWindow* parent, wxString title )
		: wxPanel( parent )
	{
		m_nBarHeight = 16;
		m_pTitleBar = new wxPanel( this, -1, wxPoint(0,0), wxSize( GetSize().x, m_nBarHeight ) );
		m_pTitleBar->SetBackgroundColour( wxColor(0x777777 ) );
		m_pClientPanel = new wxPanel( this, -1, wxPoint( 0, m_nBarHeight ), GetSize() - wxSize( 0, m_nBarHeight ) );

		wxStaticText* pSTName = new wxStaticText( m_pTitleBar, wxID_ANY, title, wxPoint( 4,2 ), wxSize( 120,m_nBarHeight ), 0, _T("ID_STATICTEXT2"));
		pSTName->SetOwnFont( wxFont( 10, wxFONTFAMILY_DEFAULT, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL ) );

		//m_pClientPanel->SetInitialSize(wxSize(GetSize().x, GetSize().y-14));
		//m_pTitleBar->SetInitialSize(wxSize(GetSize().x, 14));
	}

	void TitleWindow::OnSize( wxSizeEvent& event )
	{
		//SetInitialSize(event.GetSize());

		m_pTitleBar->SetInitialSize( wxSize( GetSize().x,  m_nBarHeight ) );	
		m_pClientPanel->SetInitialSize( wxSize( GetSize().x, GetSize().y- m_nBarHeight ) );
		( *m_pClientPanel->GetWindowChildren().begin() )->SetInitialSize( wxSize( GetSize().x, GetSize().y-14 ) );
	}
}

///------------------------------------------------------------
// CEFrame
///------------------------------------------------------------
BEGIN_EVENT_TABLE( CEFrame, wxFrame )
	EVT_CLOSE( CEFrame::OnClose )
	EVT_MENU( idMenuQuit, CEFrame::OnQuit )
	EVT_MENU( idImport, CEFrame::OnImport )
	EVT_MENU( idExport, CEFrame::OnExport )
	EVT_MENU( idMenuAbout, CEFrame::OnAbout )
	EVT_BUTTON( idBGColor, CEFrame::OnBGColor )
	EVT_TOGGLEBUTTON( idDrawCoordinate, CEFrame::OnDrawCoord ) 
	EVT_TOGGLEBUTTON( idPause, CEFrame::OnPause )
	EVT_BUTTON( idReset, CEFrame::OnReset )
END_EVENT_TABLE()

CEFrame::CEFrame( wxFrame *frame, const wxString& title )
	: wxFrame( frame, -1, title, wxDefaultPosition, wxSize( 1100,700 ) )
{

	SetMinSize( wxSize( 600, 400 ) );

	//#if wxUSE_MENUS
	// create a menu bar
	wxMenuBar* mbar = new wxMenuBar();
	wxMenu* fileMenu = new wxMenu( L"" );
	( fileMenu->Append( idImport, L"Import" ) )->Enable( false );
	fileMenu->Append( idExport, L"Export" );
	fileMenu->AppendSeparator();
	fileMenu->Append( idMenuQuit, _("&Quit\tAlt-F4"), _("Quit the application"));
	mbar->Append( fileMenu, _("&File"));

	wxMenu* helpMenu = new wxMenu(_T(""));
	helpMenu->Append(idMenuAbout, _("&About\tF1"), _("Show info about this application"));
	mbar->Append(helpMenu, _("&Help"));
	SetMenuBar(mbar);
	//#endif // wxUSE_MENUS

	//#if wxUSE_STATUSBAR
	// create a status bar with some information about the used wxWidgets version
	CreateStatusBar(2);
	SetStatusText(_(""),0);
	SetStatusText(_(""), 1);
	//#endif // wxUSE_STATUSBAR
	wxToolBar* pToolBar = new wxToolBar( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTB_HORIZONTAL | wxNO_BORDER );

	//pToolBar->AddTool(wxID_OPEN, bmpSave, L"Open"));
	wxColor btnColour( 128, 128, 192 );

	// PauseBtn
	m_pPauseBtn = new wxToggleButton(pToolBar, idPause, L"Pause", wxPoint( 0, 0 ), wxSize( 50, 25 ) );
	m_pPauseBtn->SetValue( false );
	//m_pPauseBtn->SetBackgroundColour( btnColour );
	pToolBar->AddControl( m_pPauseBtn );

	// BGColourBtn

	m_pBGColorBtn = new wxButton( pToolBar, idBGColor, L"BGC", wxPoint( 50, 0 ), wxSize( 50, 25 ) );
	m_pBGColorBtn->SetBackgroundColour( btnColour ); 
	pToolBar->AddControl( m_pBGColorBtn );
	SetToolBar( pToolBar );

	// DrawCoordBtn
	m_pDrawCoordBtn = new wxToggleButton( pToolBar, idDrawCoordinate, L"Coord", wxPoint( 100, 0 ), wxSize( 50, 25) );
	m_pDrawCoordBtn->SetValue( true );
	//m_pDrawCoordBtn->SetBackgroundColour( btnColour ); 
	pToolBar->AddControl( m_pDrawCoordBtn );

	// ResetBtn
	m_pResetBtn = new wxButton( pToolBar, idReset, L"Reset", wxPoint( 150, 0 ), wxSize( 50, 25) );
	m_pResetBtn->SetBackgroundColour( btnColour ); 
	pToolBar->AddControl( m_pResetBtn );

	// Client panel
	m_pSplitterVertical = new wxSplitterWindow( this );	
	m_pSplitterVertical->SetMinimumPaneSize( 100 );	
	m_pSplitterVertical->SetWindowStyleFlag( wxSP_LIVE_UPDATE );
	m_pSplitterHorizontal = new wxSplitterWindow( m_pSplitterVertical );	
	m_pSplitterHorizontal->SetMinimumPaneSize( 100 );
	m_pSplitterHorizontal->SetWindowStyleFlag( wxSP_LIVE_UPDATE );

	TitleWindow* pTargetViewer = new TitleWindow( m_pSplitterHorizontal,L"TargetViewer" );
	TitleWindow* pEmitterList = new TitleWindow( m_pSplitterVertical,L"EmitterList" );
	TitleWindow* pPropertiesPane = new TitleWindow( m_pSplitterHorizontal,L"PropertiesPane" );

	m_pEmitterList = new CEEmitterList( pEmitterList->GetClientPanel() );
	m_pTargetViewer = new CETargetViewer( pTargetViewer->GetClientPanel() );
	m_pPropertiesPane = CEGlobalCreatePropertiesPane( pPropertiesPane->GetClientPanel() );

	m_pSplitterHorizontal->SplitHorizontally( pTargetViewer, pPropertiesPane, 400 );
	m_pSplitterVertical->SplitVertically( m_pSplitterHorizontal, pEmitterList, 440 );
}

CEFrame::~CEFrame()
{

}

void CEFrame::OnClose( wxCloseEvent &event )
{
	Destroy();
}

void CEFrame::OnQuit( wxCommandEvent &event )
{
	Destroy();
}

void CEFrame::OnAbout( wxCommandEvent &event )
{
	//SetViewerClearColor();
}

void CEFrame::OnBGColor( wxCommandEvent &event )
{
	wxColourDialog cd( this );
	if ( wxID_OK == cd.ShowModal() )
	{
		wxColor color = cd.GetColourData().GetColour();
		XMFLOAT4 vColor( (float)color.Red() / 255, 
			(float)color.Green() / 255,
			(float)color.Blue() / 255, 
			1.0f );
		CEGetCascadeSystem()->SetBGColor( vColor );
	}
}

void CEFrame::OnDrawCoord( wxCommandEvent & event )
{
	CEGetCascadeSystem()->SetDrawCoordinate( m_pDrawCoordBtn->GetValue() );
}

void CEFrame::OnPause( wxCommandEvent &event )
{
	CEGetCascadeSystem()->SetPause( m_pPauseBtn->GetValue() );
}

void CEFrame::OnReset( wxCommandEvent &event )
{
	CEGetCascadeSystem()->InitEmitter();
}

void CEFrame::OnImport( wxCommandEvent &event )
{
	
}

void CEFrame::OnExport( wxCommandEvent & event )
{
	wxFileDialog* pFD = new wxFileDialog( this );
	pFD->SetWindowStyle( wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
	pFD->SetWildcard( L"SPF files (*.spf)|*.spf" );	
	pFD->ShowModal();

	if ( pFD->GetPath() != L"" )
	{
		if ( pFD->GetPath().EndsWith( ".spf" ) )
		{	
			CEGetCascadeSystem()->Export( pFD->GetPath() );
		}
		else
		{
			wxMessageBox( L"The file is not available", L"Error" ); 
		}
	}
}
/*
void CEFrame::SetViewerClearColor()
{

}
*/